package com.itheima.service;

import com.itheima.mapper.GoodsMapper;
import com.itheima.pojo.Goods;
import com.itheima.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class GoodsService {

    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 添加商品数据
     * @param goods
     */
    public void add(Goods goods) {
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取GoodsMapper
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        //4. 调用方法
        mapper.add(goods);

        //5. 提交事务
        sqlSession.commit();

        sqlSession.close();
    }

    /**
     * 查询所有
     * @return
     */
    public List<Goods> findAll(){

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取GoodsMapper
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        //4. 调用方法
        List<Goods> goodsList = mapper.findAll();

        sqlSession.close();

        return goodsList;
    }
}
